//
//  SEMyRegistrationListCell.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/15.
//

import UIKit

enum SEMyRegistrationListCellStatus: Int {
    case cancel = 0
    case success

    var title: String {
        switch self {
        case .cancel: return "已取消"
        case .success: return "预约成功"
        }
    }

    var titleColor: UIColor {
        switch self {
        case .cancel:  return Asset.textGrayLightest.color
        case .success: return Asset.main.color
        }
    }

    var cancelBtnBottom: UILayoutPriority {
        switch self {
        case .cancel: return .defaultLow
        case .success: return .defaultHigh
        }
    }

    var stackBgViewBottom: UILayoutPriority {
        switch self {
        case .cancel: return .defaultHigh
        case .success: return .defaultLow
        }
    }
}

protocol SEMyRegistrationListCellDelegate: NSObjectProtocol {
    func myRegistrationListCellCancelAction(indexPath: IndexPath)
}

class SEMyRegistrationListCell: UITableViewCell {

    static let className = "SEMyRegistrationListCell"
    static let reuseId = className

    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var doctorNameLabel: UILabel!
    @IBOutlet weak var jobTitleLabel: UILabel!
    @IBOutlet weak var hospitalNameLabel: UILabel!
    @IBOutlet weak var deptNameLabel: UILabel!
    @IBOutlet weak var patientNameLabel: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var stackBgViewBottom: NSLayoutConstraint!
    @IBOutlet weak var cancelBtnBottom: NSLayoutConstraint!
    @IBOutlet weak var stackBgView: UIView! {
        didSet {
            stackBgView.layer.cornerRadius = 4
            stackBgView.layer.borderWidth = 1
            stackBgView.layer.borderColor = Asset.main.color.withAlphaComponent(0.15).cgColor
            stackBgView.layer.masksToBounds = true
        }
    }
    @IBOutlet weak var cancelBtn: UIButton! {
        didSet {
            cancelBtn.layer.cornerRadius = cancelBtn.frame.size.height / 2.0
            cancelBtn.layer.borderColor = Asset.main.color.cgColor
            cancelBtn.layer.borderWidth = 1
            cancelBtn.layer.masksToBounds = true
        }
    }

    @IBAction func cancelBtnAction(_ sender: UIButton) {
        delegate?.myRegistrationListCellCancelAction(indexPath: indexPath)
    }

    var indexPath = IndexPath(row: 0, section: 0)
    weak var delegate: SEMyRegistrationListCellDelegate?

    var status: SEMyRegistrationListCellStatus = .success {
        didSet {
            statusLabel.text = status.title
            statusLabel.textColor = status.titleColor
            cancelBtnBottom.priority = status.cancelBtnBottom
            stackBgViewBottom.priority = status.stackBgViewBottom
            cancelBtn.isHidden = status == .cancel
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func updateCell(model: SEMyRegistrationModel, indexPath: IndexPath) {
        self.indexPath = indexPath
        self.status = SEMyRegistrationListCellStatus(rawValue: model.flag) ?? .cancel
        doctorNameLabel.text = model.doctorName
        jobTitleLabel.text = model.jobTitle
        hospitalNameLabel.text = model.hospitalName
        deptNameLabel.text = model.deptName
        patientNameLabel.text = "就诊人: \(model.patientName)"
        dateLabel.text = "就诊日期: \(model.diagDate)"
        timeLabel.text = "候诊时间: \(model.diagTime) 序号\(model.number)"
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
